package com.example.demo.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.domain.*;
import com.example.demo.service.UserService;
import com.example.demo.mapper.UserMapper;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @author Administrator
 * @description 针对表【user】的数据库操作Service实现
 * @createDate 2025-07-11 14:12:58
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
        implements UserService {

    @Override
    public boolean register(UserRegisterReq userRegisterReq) {
        String username = userRegisterReq.getUsername();
        String password = userRegisterReq.getPassword();
        String checkPassword = userRegisterReq.getCheckPassword();

        if (StrUtil.isBlank(username) || username.length() > 8) {
            return false;
        }

        if (StrUtil.isBlank(password) || password.length() > 12) {
            return false;
        }

        if (!password.equals(checkPassword)) {
            return false;
        }

        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername, username);

        User one = getOne(queryWrapper);
        if (one != null) {
            return false;
        }

        User user = new User();
        user.setUsername(username);
        user.setPassword(password);

        return save(user);
    }

    @Override
    public LoginUserResp login(UserLoginReq userLoginReq) {
        LoginUserResp loginUserResp = new LoginUserResp();

        String username = userLoginReq.getUsername();
        String password = userLoginReq.getPassword();
        if (StrUtil.isBlank(username) || username.length() > 8) {
            return null;
        }

        if (StrUtil.isBlank(password) || password.length() > 12) {
            return null;
        }


        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername, username)
                .eq(User::getPassword, password);

        User user = getOne(queryWrapper);
        if (user == null) {
            return null;
        }

        loginUserResp.setId(user.getId());
        loginUserResp.setIsVip(user.getIsVip());
        loginUserResp.setVipNumber(user.getVipNumber());
        loginUserResp.setNickname(user.getNickname());
        loginUserResp.setVipExpireTime(user.getVipExpireTime());
        loginUserResp.setRole(user.getRole());
        // 改为satoken 登录
        StpUtil.login(user.getId());
        loginUserResp.setToken(StpUtil.getTokenInfo().tokenValue);
        StpUtil.getSession().set("login_user", loginUserResp);
//        httpServletRequest.getSession().setAttribute("login_user",loginUserResp);

        return loginUserResp;
    }

    @Override
    public SearchUserResp search(Long id) {
        if (id == null || id < 1) {
            return null;
        }

        User user = getById(id);
        if (user == null) {
            return null;
        }

        return SearchUserResp.convert(user);
    }

    @Override
    public Boolean delete(Long id) {
        if (id == null || id < 1) {
            return false;
        }

        User user = getById(id);
        // 用户为空或用户为管理员 无法删除
        if (user == null || Objects.equals(user.getRole(), "admin")) {
            return false;
        }

        return removeById(id);
    }

    @Override
    public Boolean update(UpdateUserReq updateUserReq) {
        if (updateUserReq == null) {
            return false;
        }

        Long id = updateUserReq.getId();
        String phone = updateUserReq.getPhone();
        String email = updateUserReq.getEmail();

        if (id == null || id < 1) {
            return false;
        }
        User user = getById(id);
        if (user == null) {
            return false;
        }

        user.setPhone(phone);
        user.setEmail(email);
        return updateById(user);
    }
}




